cmake_minimum_required(VERSION 3.1.3)
project("user-server")
set(CMAKE_CXX_STANDARD 17)

set(src_path "${CMAKE_CURRENT_SOURCE_DIR}/source")
file(GLOB src_files "${src_path}/*.cc")

set(proto_path "${CMAKE_CURRENT_SOURCE_DIR}/../proto")
set(proto_file_names "user.proto" "base.proto" "file.proto")

set(odb_file_names "user.hxx")
set(odb_path "${CMAKE_CURRENT_SOURCE_DIR}/../odb")
set(CMAKE_BUILD_TYPE "Debug")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0 -ggdb")
foreach (proto_file_name ${proto_file_names})
    string(REPLACE ".proto" ".pb.cc" proto_cc ${proto_file_name})
    if(NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}/${proto_cc})
        add_custom_command(
            PRE_BUILD
            COMMAND protoc
            ARGS --cpp_out=${CMAKE_CURRENT_BINARY_DIR} --proto_path=${proto_path} "${proto_path}/${proto_file_name}"
            DEPENDS "${proto_path}/${proto_file_name}"
            OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${proto_cc}"
            COMMENT "生成proto框架代码文件: ${proto_cc}"
        )
    endif()
    list(APPEND proto_ccs "${CMAKE_CURRENT_BINARY_DIR}/${proto_cc}")
endforeach()

foreach (odb_file_name ${odb_file_names})
    string(REPLACE ".hxx" "-odb.hxx" odb_hxx ${odb_file_name})
    string(REPLACE ".hxx" "-odb.cxx" odb_cxx ${odb_file_name})
    if(NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}/odb_cxx)
        add_custom_command(
            PRE_BUILD
            COMMAND odb
            ARGS -d mysql --std c++17 --generate-query --generate-schema --profile boost/date-time "${odb_path}/${odb_file_name}"
            DEPENDS "${odb_path}/${odb_file_name}"
            OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${odb_cxx}
            COMMENT "生成odb框架代码文件: ${odb_cxx}"
        )
    endif()
    list(APPEND odb_srcs ${CMAKE_CURRENT_BINARY_DIR}/${odb_cxx})
endforeach()

add_executable(user_server ${src_files} ${proto_ccs} ${odb_srcs})

# 测试客户端
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/test/user_client.cc")
    message(STATUS "Found test client files, adding test client executable.")
    file(GLOB user_client_file "${CMAKE_CURRENT_SOURCE_DIR}/test/*.cc")
    add_executable(user_client ${user_client_file} ${proto_ccs})
    target_link_libraries(user_client brpc gtest leveldb gflags ssl curl crypto protobuf etcd-cpp-api cpprest fmt jsoncpp spdlog gtest)
endif()


include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../common)
include_directories(${CMAKE_CURRENT_BINARY_DIR})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../odb)

target_link_libraries(user_server brpc leveldb gflags ssl curl crypto protobuf etcd-cpp-api cpprest fmt jsoncpp spdlog fmt odb-mysql odb odb-boost redis++ hiredis gflags -pthread elasticlient cpr jsoncpp)

install(TARGETS user_server RUNTIME DESTINATION bin)